Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

weapon_reorder.js ➔ TableDnD   F
last analyzed

Complexity

Conditions 20

Size

Total Lines 79
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 59
dl 0
loc 79
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like weapon_reorder.js ➔ TableDnD often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// WWW: http://www.isocra.com/
2
3
var currenttable = null;
4
5
document.onmousemove = function(ev) {
6
	if (currenttable && currenttable.dragObject) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if currenttable && currenttable.dragObject is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
7
		ev = ev || window.event;
8
		var mousePos = currenttable.mouseCoords(ev);
9
		var y = mousePos.y - currenttable.mouseOffset.y;
10
11
		var yOffset = window.pageYOffset;
12
		var currentY = mousePos.y;
13
		if (document.all) {
14
			yOffset=document.body.scrollTop;
15
			currentY = event.clientY;
0 ignored issues
show
Bug introduced by
The variable event seems to be never declared. If this is a global, consider adding a /** global: event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
16
		}
17
		if (currentY-yOffset < 5) {
18
				window.scrollBy(0, -5);
19
		} else {
20
			var windowHeight = window.innerHeight ? window.innerHeight
21
					: document.documentElement.clientHeight ? document.documentElement.clientHeight
22
							: document.body.clientHeight;
23
			if (windowHeight-currentY-yOffset < 5) {
24
				window.scrollBy(0, 5);
25
			}
26
		}
27
28
		if (y != currenttable.oldY) {
29
			var movingDown = y > currenttable.oldY;
30
			currenttable.oldY = y;
31
			currenttable.dragObject.style.backgroundColor.value="#aaa";
32
			var currentRow = currenttable.findDropTargetRow(y);
33
			if (currentRow) {
34
				if (movingDown && currenttable.dragObject != currentRow) {
35
					currenttable.dragObject.parentNode.insertBefore(currenttable.dragObject, currentRow.nextSibling);
36
				}
37
				else if (! movingDown && currenttable.dragObject != currentRow) {
38
					currenttable.dragObject.parentNode.insertBefore(currenttable.dragObject, currentRow);
39
				}
40
			}
41
		}
42
43
		return false;
44
	}
45
};
46
47
document.onmouseup = function(ev){
0 ignored issues
show
Unused Code introduced by
The parameter ev is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
48
	if (currenttable && currenttable.dragObject) {
49
		var droppedRow = currenttable.dragObject;
50
		droppedRow.style.backgroundColor = 'transparent';
51
		currenttable.dragObject = null;
52
		currenttable.onDrop(currenttable.table, droppedRow);
53
		currenttable = null;
54
	}
55
};
56
57
function getEventSource(evt) {
58
	if (window.event) {
59
		evt = window.event;
60
		return evt.srcElement;
61
	} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
62
		return evt.target;
63
	}
64
}
65
66
function TableDnD() {
67
	this.dragObject = null;
68
	this.mouseOffset = null;
69
	this.table = null;
70
	this.oldY = 0;
71
72
	this.init = function(table) {
73
		this.table = table;
74
		var rows = table.tBodies[0].rows;
75
		for (var i=0; i<rows.length; i++) {
76
			var nodrag = rows[i].getAttribute("NoDrag");
77
			if (nodrag == null || nodrag == "undefined") {
0 ignored issues
show
Best Practice introduced by
Comparing nodrag to null using the == operator is not safe. Consider using === instead.
Loading history...
78
				this.makeDraggable(rows[i]);
79
			}
80
		}
81
	};
82
83
	this.onDrop = function(table, droppedRow) {};
0 ignored issues
show
Unused Code introduced by
The parameter droppedRow is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter table is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
84
85
	this.getPosition = function(e) {
86
		var left = 0;
87
		var top = 0;
88
		if (e.offsetHeight == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing e.offsetHeight to 0 using the == operator is not safe. Consider using === instead.
Loading history...
89
			e = e.firstChild;
90
		}
91
		while (e.offsetParent) {
92
			left += e.offsetLeft;
93
			top += e.offsetTop;
94
			e = e.offsetParent;
95
		}
96
		left += e.offsetLeft;
97
		top += e.offsetTop;
98
		return {x:left, y:top};
99
	};
100
	this.mouseCoords = function(ev) {
101
		if(ev.pageX || ev.pageY) {
102
			return {x:ev.pageX, y:ev.pageY};
103
		}
104
		return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop - document.body.clientTop};
105
	};
106
	this.getMouseOffset = function(target, ev) {
107
		ev = ev || window.event;
108
		var docPos = this.getPosition(target);
109
		var mousePos = this.mouseCoords(ev);
110
		return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
111
	};
112
	this.makeDraggable = function(item) {
113
		if(!item) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
114
		var self = this;
115
		item.onmousedown = function(ev) {
116
			var target = getEventSource(ev);
117
			if (target.tagName == 'INPUT' || target.tagName == 'SELECT') return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
118
			currenttable = self;
119
			self.dragObject = this;
120
			self.mouseOffset = self.getMouseOffset(this, ev);
121
			return false;
122
		};
123
		item.style.cursor="move";
124
	};
125
	this.findDropTargetRow = function(y) {
126
		var rows = this.table.tBodies[0].rows;
127
		for (var i=0; i<rows.length; i++) {
128
			var row = rows[i];
129
			var nodrop = row.getAttribute("NoDrop");
130
			if (nodrop == null || nodrop == "undefined") {
0 ignored issues
show
Best Practice introduced by
Comparing nodrop to null using the == operator is not safe. Consider using === instead.
Loading history...
131
				var rowY	= this.getPosition(row).y;
132
				var rowHeight = parseInt(row.offsetHeight)/2;
133
				if (row.offsetHeight == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing row.offsetHeight to 0 using the == operator is not safe. Consider using === instead.
Loading history...
134
					rowY = this.getPosition(row.firstChild).y;
135
					rowHeight = parseInt(row.firstChild.offsetHeight)/2;
136
				}
137
				if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
138
					return row;
139
				}
140
			}
141
		}
142
		return null;
143
	};
144
}
145
146
var table = document.getElementById('weapon_reorder');
147
var tableDnD = new TableDnD();
148
tableDnD.init(table);
149
150
function moveRow(cell, move) {
151
	var currentRow = cell.parentNode;
152
	var currentRowID = false;
153
	var rows = currentRow.parentNode.rows;
154
	for(var i = 1; i < rows.length; i++) {
155
		if(rows[i] == currentRow) currentRowID = i;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
156
	}
157
	if(currentRowID==false) return;
0 ignored issues
show
Best Practice introduced by
Comparing currentRowID to false using the == operator is not safe. Consider using === instead.
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
158
	if(move>0)
159
		move++;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
160
	var newRowID = currentRowID+move;
161
	if(newRowID>rows.length)
162
		newRowID = 1;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
163
	else if(newRowID<1)
164
		newRowID = rows.length;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
165
166
	currentRow.parentNode.insertBefore(currentRow, rows[newRowID]);
167
};
168
169
function doSubmit() {
170
	var rows = document.getElementById('weapon_reorder').rows;
171
	var ret = new Array();
0 ignored issues
show
Coding Style Best Practice introduced by
Using the Array constructor is generally discouraged. Consider using an array literal instead.
Loading history...
172
	for(var i = 0; i < rows.length;i++) {
173
		ret[ret.length] = rows[i].getElementsByTagName('td')[0].innerHTML;
174
	}
175
	return ret.join('|');
176
}
177